package com.cjg.ayn;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * 异步调用 CompletableFuture
 * //异步执行
 * 成功回调
 * 失败回调
 */
public class futureDemo01 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
   /*     CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {

            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        });

        System.out.println("sdjfak");
        voidCompletableFuture.get(); //执行异步回调的任务
    }*/

        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            int i=10/0;
            return 1024;
        });

        Integer integer = integerCompletableFuture.whenComplete((t, u) -> {
            System.out.println(t);
            System.out.println(u);
        }).exceptionally((e) -> {
            System.out.println(e.getMessage());
            return 04545;
        }).get();//get()执行 函数是接口内容
        System.out.println(integer);


    }
}